package com.itheima.appserver.service;

import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.appserver.excepiton.ErrInfo;
import com.itheima.appserver.excepiton.ErroResult;
import com.itheima.appserver.excepiton.MyEx;
import org.bson.types.ObjectId;

import com.itheima.appserver.config.MyThreadLocal;
import com.itheima.commons.constants.Constants;
import com.itheima.commons.pojo.FollowUser;
import com.itheima.commons.pojo.Video;
import com.itheima.dubbo.interfaces.PublishApi;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.pojo.Comment;
import com.itheima.commons.vo.PageResult;
import com.itheima.dubbo.interfaces.CommentApi;
import com.itheima.dubbo.interfaces.VideoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: lqTao
 * @Date: 2021/12/11 12:59
 */
@Service
public class VideoService {

    @Autowired
    protected FastFileStorageClient storageClient;

    @Reference
    VideoApi videoApi;

    @Autowired
    CommentService commentService;

    @Reference
    CommentApi commentApi;

    @Autowired
    PublishService publishService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    IdService idService;

    @Autowired
    FdfsWebServer fdfsWebServer;



    //查询视频
    public PageResult getVideo(Integer page, Integer pagesize) {
        PageResult pageResult = publishService.recommendMovements(page, pagesize, Video.class);
        return pageResult;
    }

    public void saveVideo(MultipartFile picFile, MultipartFile videoFile) {
// 先判断小视频的文件是否为空 如果为空就直接抛出"一个小视频内容不能为空"的异常
        // 判断 小视频 和  封面图片的 格式
        // 判断 小视频 和  封面图片的 大小
        // 把图片 和 小视频 上传到FastDFS中
        // 先上传封面图片文件  这个地方我没有校验
        try {
            StorePath picPath = this.storageClient.uploadFile(picFile.getInputStream(), picFile.getSize(), StrUtil.subAfter(picFile.getOriginalFilename(), ".", true), null);
            StorePath videoPath = this.storageClient.uploadFile(videoFile.getInputStream(), videoFile.getSize(), StrUtil.subAfter(videoFile.getOriginalFilename(), ".", true), null);

            Video video = new Video();
            // 主键ID
            video.setId(new ObjectId());
            // 用于大数据推荐的VID
            video.setVid(idService.createId("VIDEO"));
            // 上传小视频的人的ID
            video.setUserId(MyThreadLocal.getUserId());
            // 小视频的配文
            video.setText("可以不写");
            // 封面图片地址
            video.setPicUrl(fdfsWebServer.getWebServerUrl() + picPath.getFullPath());
            // 小视频地址
            video.setVideoUrl(fdfsWebServer.getWebServerUrl() + videoPath.getFullPath());
            // 发布时间
            video.setCreated(System.currentTimeMillis());
            // 调用Dubbo实现小视频的发布
            videoApi.saveVideo(video);
        } catch (Exception e) {
            throw new MyEx(ErroResult.fail(ErrInfo.videoError));
        }
    }

    public Long likeVideo(String videoId, Class t) {
        Long aLong = commentService.comment(videoId, 1, t);

        //把圈子点赞数存入redis
        redisTemplate.opsForHash().put(Constants.MOVEMENTS_INTERACT_KEY + videoId, Constants.VIDEO_LIKE_HASHKEY, aLong.toString());
        //把圈子点赞状态存入redis
        redisTemplate.opsForHash().put(Constants.MOVEMENTS_INTERACT_KEY + videoId, Constants.VIDEO_ISLIKE_HASHKEY + MyThreadLocal.getUserId(), "1");

        return aLong;
    }

    public Long dislikeVideo(String videoId, Class t) {
        Long aLong = commentService.disComment(videoId, 1, t);

        //把圈子点赞数存入redis
        redisTemplate.opsForHash().put(Constants.MOVEMENTS_INTERACT_KEY + videoId, Constants.VIDEO_LIKE_HASHKEY, aLong.toString());
        //把圈子点赞状态从redis删除
        redisTemplate.opsForHash().delete(Constants.MOVEMENTS_INTERACT_KEY + videoId, Constants.VIDEO_ISLIKE_HASHKEY + MyThreadLocal.getUserId());

        return aLong;
    }

    public void commentVideo(String videoId, String commentText) {
        //调用comment里封装的方法
        Comment comment = commentService.setComment(videoId, 2);
        comment.setContent(commentText);
        commentApi.saveComment(comment);
    }


    public Long likeCommentVideo(String videoCommentId, Class t) {
        Long aLong = this.likeVideo(videoCommentId, t);
        return aLong;
    }

    public Long dislikeCommentVideo(String videoCommentId, Class t) {
        Long aLong = this.dislikeVideo(videoCommentId, t);
        return aLong;
    }

    public void userFocus(Long uid) {
        FollowUser followUser = new FollowUser();
        followUser.setUserId(MyThreadLocal.getUserId());
        followUser.setFollowUserId(uid);
        followUser.setCreated(System.currentTimeMillis());
        videoApi.followUser(followUser);

        //redis中设置关注缓存
        redisTemplate.opsForSet().add(Constants.FOLLOW_USER+MyThreadLocal.getUserId(),uid.toString());
    }

    public void userFocusUn(Long uid) {
        FollowUser followUser = new FollowUser();
        followUser.setUserId(MyThreadLocal.getUserId());
        followUser.setFollowUserId(uid);
        videoApi.disFollowUser(followUser);
        //redis中取消关注缓存
        redisTemplate.opsForSet().remove(Constants.FOLLOW_USER+MyThreadLocal.getUserId(),uid.toString());

    }

    public PageResult queryCommentList(String videoId, Integer page, Integer pageSize) {
        PageResult pageResult = commentService.queryComment(videoId, page, pageSize, Video.class);
        return pageResult;
    }
}
